﻿using System.Windows.Forms;

namespace GBT.Client.FileOperation
{
    public static class ListViewSort
    {
        /// <summary>
        /// 为listView添加列点击排序逻辑，可在FormLoad()事件中为指定的listView添加排序逻辑
        /// </summary>
        public static void AddColumnSort(this ListView list)
        {
            list.ListViewItemSorter = new ListViewColumnSorter();
            list.ColumnClick += new ColumnClickEventHandler(ListView_ColumnClick);
        }

        ///// <summary>
        ///// 构造函数
        ///// </summary>
        //public ListViewSort()
        //{
        //    //
        //    // TODO: 在此处添加构造函数逻辑
        //    //
        //}

        public static void ListView_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            ListView lv = sender as ListView;

            // 检查点击的列是不是现在的排序列.
            if (e.Column == (lv.ListViewItemSorter as ListViewColumnSorter).SortColumn)
            {
                // 重新设置此列的排序方法.
                if ((lv.ListViewItemSorter as ListViewColumnSorter).Order == SortOrder.Ascending)
                {
                    (lv.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Descending;
                }
                else
                {
                    (lv.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Ascending;
                }
            }
            else
            {
                // 设置排序列，默认为正向排序
                (lv.ListViewItemSorter as ListViewColumnSorter).SortColumn = e.Column;
                (lv.ListViewItemSorter as ListViewColumnSorter).Order = SortOrder.Ascending;
            }
            // 用新的排序方法对ListView排序
            ((ListView)sender).Sort();
        }
    }
}
